package com.example.jiaju.ui;

import android.graphics.Rect;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.jiaju.DataApplication;
import com.example.jiaju.R;
import com.example.jiaju.adapter.BBSAdapter;
import com.example.jiaju.adapter.ExpertAdapter;
import com.example.jiaju.api.BBSApi;
import com.example.jiaju.api.FurnitureApi;
import com.example.jiaju.entity.BBS;
import com.example.jiaju.entity.Expert;
import com.example.jiaju.entity.User;
import com.example.jiaju.utils.MyItemDecoration;
import com.example.jiaju.utils.RetrofitUtils;
import com.google.android.material.card.MaterialCardView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 楼盘
 */
public class BuildingFragment extends Fragment {


    private ExpertAdapter expertAdapter;
    private RecyclerView expertRecyclerView;
    private BBSAdapter adapter;
    private RecyclerView teaseRecyclerView;

    private List<Expert> expertList=new ArrayList<>();

    public List<BBS.PageDTO> bbsList;
    public List<User.PageDTO.ListDTO> userList;

    public BuildingFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_building, container, false);

//        //构造假数据
        Expert expert1=new Expert("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202212051734503.png","https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202212051734503.png","杨小花");
        Expert expert2=new Expert("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202212051735852.png","https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202212051735852.png","卡达尔");
        Expert expert3=new Expert("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202212051737103.png","https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202212051737103.png","葛华");
        expertList.add(expert1);
        expertList.add(expert2);
        expertList.add(expert3);

        expertAdapter=new ExpertAdapter(expertList,getContext());
        expertRecyclerView=view.findViewById(R.id.build_expert_recyclerView);
        expertRecyclerView.setAdapter(expertAdapter);
        expertRecyclerView.addItemDecoration(new MyItemDecoration(8));//设置item间距
        expertRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(),
                LinearLayoutManager.HORIZONTAL,false));

        //先查用户ID，在查用户发布的bbs

        //用户吐槽 楼盘BBS
        BBSApi api= RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(BBSApi.class);
        Call<BBS> call=api.getBBSByKind(1,1,5);
        call.enqueue(new Callback<BBS>() {
            @Override
            public void onResponse(Call<BBS> call, Response<BBS> response) {
                BBS bbs=response.body();
                //填充吐槽信息
                if(bbs==null){
                    Toast.makeText(getContext(),"楼盘数据加载异常！",Toast.LENGTH_SHORT).show();
                }else{
                    bbsList=bbs.page;
                    adapter=new BBSAdapter(bbsList);
                    teaseRecyclerView=view.findViewById(R.id.build_tease_recyclerView);
                    teaseRecyclerView.setAdapter(adapter);
                    teaseRecyclerView.addItemDecoration(new MyItemDecoration(12));
                    teaseRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<BBS> call, Throwable t) {

            }
        });

        return view;
    }







}
